<template>
  <div class="checkout-address">
    <div class="text">
      <div class="none" v-if="!showAddress">您需要先添加收货地址才可提交订单。</div>
      <ul v-if="showAddress">
        <li><span>收<i />货<i />人：</span>{{ showAddress.receiver }}</li>
        <li><span>联系方式：</span>{{ hiddenContact(showAddress.contact )}}</li>
        <li><span>收货地址：</span>{{ showAddress.fullLocation.replace(/ /g, '') }}</li>
      </ul>
      <a href="javascript:;" v-if="showAddress"  @click="openAddressEdit(showAddress)">修改地址</a>
    </div>
    <div class="action">
      <XtxButton class="btn" @click="openDialog">切换地址</XtxButton>
      <XtxButton class="btn" @click="openAddressEdit(null)">添加地址</XtxButton>
    </div>
  </div>

  <XtxDialog title="切换收货地址" v-model:visible="visibleDialog">
    <template #default>
      <div class="text item" @click="selectedAddress = item" :class="{ active: selectedAddress?.id === item.id }"
        v-for="item in list.slice(0, 5)" :key="item.id">
        <ul>
          <li><span>收<i />货<i />人：</span>{{item.receiver}}</li>
          <li><span>联系方式：</span>{{ item.contact }}</li>
          <li><span>收货地址：</span>{{ item.fullLocation.replace(/ /g,'')}}</li>
        </ul>
      </div>
    </template>
    <template #footer>
      <XtxButton type="gray" style="margin-right:20px" @click="visibleDialog = false">取消</XtxButton>
      <XtxButton type="primary" @click="confirmAddress">确认</XtxButton>
    </template>
  </XtxDialog>
  <!-- 添加/编辑 -->
  <AddressEdit ref="addressEditCom" @onSuccess="successHandler"></AddressEdit>
</template>
<script>
import { ref } from 'vue'
import AddressEdit from './AddressEdit'

export default {
  name: 'CheckoutAddress',
  emits: ['change'],
  components: {
    AddressEdit
  },
  props: {
    list: {
      type: Array,
      default: () => ([])
    }
  },

  setup (props, context) {
    const showAddress = ref(null)
    const visibleDialog = ref(false)
    const defaultAddress = props.list?.find(item => item.isDefault === 0)
    if (defaultAddress) {
      showAddress.value = defaultAddress
    } else {
      if (props.list.length) {
        // eslint-disable-next-line vue/no-setup-props-destructure
        showAddress.value = props.list[0]
      }
    }
    function hiddenContact (str) {
      return str.substr(0, 3) + '****' + str.substr(7)
    }

    const selectedAddress = ref(null)

    function confirmAddress () {
      showAddress.value = selectedAddress.value
      context.emit('change', showAddress.value && showAddress.value.id)
      visibleDialog.value = false
    }

    function openDialog () {
      visibleDialog.value = true
      selectedAddress.value = null
    }

    const addressEditCom = ref(null)
    function openAddressEdit (address) {
      addressEditCom.value.open(address)
    }

    function successHandler (newAddress) {
      const address = props.list.find(item => item.id === newAddress.id)
      if (address) {
        for (const key in address) {
          address[key] = newAddress[key]
        }
      } else {
        const addressJson = JSON.stringify(newAddress)
        // eslint-disable-next-line vue/no-mutating-props
        props.list.unshift(JSON.parse(addressJson))
      }
    }

    // 收货地址通知父组件
    context.emit('change', showAddress.value && showAddress.value.id)

    return {
      hiddenContact, showAddress, visibleDialog, selectedAddress, confirmAddress, openDialog, openAddressEdit, addressEditCom, successHandler
    }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  .text {
    flex: 1;
    display: flex;
    align-items: center;

    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;

      &.active,
      &:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor, 50%);
      }

      >ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}

.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;

  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;

    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }

    >ul {
      flex: 1;
      padding: 20px;

      li {
        line-height: 30px;

        span {
          color: #999;
          margin-right: 5px;

          >i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }

    >a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }

  .action {
    width: 420px;
    text-align: center;

    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;

      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
